import Vue from "vue";
import VueRouter from "vue-router";

import iView from "view-design";

import util from "@/libs/util";

import Setting from "@/setting";

import store from "@/store/index";

// 路由数据
import routes from "./routes";


Vue.use(VueRouter);

// 导出路由 在 main.js 里使用
const router = new VueRouter({
  routes,
  mode: Setting.routerMode,
  base: Setting.routerBase
});

/**
 * 路由拦截
 * 权限验证
 */
let that = this

router.beforeEach((to, from, next) => {
//to到哪儿  from从哪儿离开  next跳转 为空就是放行
  if (to.path === '/login'||to.path === '/register') {
    //如果跳转为登录，就放行
    next();
  } else {
    //取出localStorage判断
    let token = localStorage.getItem("token");
    if (token == null || token === '') {
      // 引用elementui message提示框
      // Message({
      //   message: '没有身份验证，请先登录',
      //   type: 'error'
      // })
      alert('没有身份验证，请先登录');
      next({name: 'login'});
    } else {
      next();
    }
  }});

router.afterEach(to => {
  if (Setting.showProgressBar) iView.LoadingBar.finish();

  // 多页控制 打开新的页面
  store.dispatch("admin/page/open", to);
  // 更改标题
  util.title({ title: to.meta.title });
  // 返回页面顶端
  window.scrollTo(0, 0);
});

export default router;


